package com.wwtech.epp.ui.activity.login;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.ninjia.ninjiabaselibrary.utils.SpUtil;
import com.socks.library.KLog;
import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.base.BaseActivity;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.ui.activity.login.component.DaggerLoginComponent;
import com.wwtech.epp.ui.activity.login.contract.LoginContract;
import com.wwtech.epp.ui.activity.login.module.LoginModule;
import com.wwtech.epp.ui.activity.login.presenter.LoginPresenter;
import com.wwtech.epp.ui.activity.main.MainActivity;
import com.wwtech.epp.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.refactor.library.SmoothCheckBox;
import io.rong.imkit.RongIM;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.login
 * @Description: $description
 * @date 2017/08/01 17:11:43
 */

public class LoginActivity extends BaseActivity implements LoginContract.View {

    @Inject
    LoginPresenter mPresenter;
    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.bt_login)
    Button btLogin;
    @BindView(R.id.clear_phone)
    ImageView clearPhone;
    @BindView(R.id.clear_psd)
    ImageView clearPsd;
    @BindView(R.id.avater)
    ImageView avater;
    @BindView(R.id.check)
    SmoothCheckBox check;
    @BindView(R.id.checkview)
    View checkview;
    private int clickCount = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        setTitle("登录");
    }

    @Override
    protected void initData() {
        AppConfig.getInstance().mAppActivityManager.finishAllActivityWithoutThis();
        try {
            RongIM.getInstance().logout();
        } catch (Exception e) {
            e.printStackTrace();
        }
        check.setChecked(SpUtil.getBoolean(this, ContainValue.ENVIRONMENT, true));
        check.setOnCheckedChangeListener(new SmoothCheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SmoothCheckBox smoothCheckBox, boolean b) {
                if (b) {
                    ToastUtil.show(LoginActivity.this, "选择线上环境");
//                    SpUtil.putBoolean(AppConfig.getInstance(), ContainValue.ENVIRONMENT, true);
                } else {
                    ToastUtil.show(LoginActivity.this, "选择测试环境");
//                    SpUtil.putBoolean(AppConfig.getInstance(), ContainValue.ENVIRONMENT, false);
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (check.getVisibility() == View.VISIBLE) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("环境已经修改，点击确定生效");
            builder.setNegativeButton("取消", null);
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (check.isChecked()) {
                        SpUtil.putBoolean(AppConfig.getInstance(), ContainValue.ENVIRONMENT, true);
                        KLog.i("选择了线上环境，置为true");
                    } else {
                        SpUtil.putBoolean(AppConfig.getInstance(), ContainValue.ENVIRONMENT, false);
                        KLog.i("选择了开发环境，置为false");
                    }
                    AppConfig.getInstance().mAppActivityManager.AppExit();
                }
            });
            builder.show();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void setupActivityComponent() {
        DaggerLoginComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .loginModule(new LoginModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(LoginContract.LoginContractPresenter presenter) {
        mPresenter = (LoginPresenter) presenter;
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public void loginSuccess() {
        SpUtil.putString(this, ContainValue.LOGINPHONE, etAccount.getText().toString().trim());
        SpUtil.putString(this, ContainValue.LOGINPASSWORD, etPassword.getText().toString().trim());
        startActivity(MainActivity.class);
        AppConfig.getInstance().mAppActivityManager.finishActivity();
    }

    @OnClick(R.id.bt_login)
    public void onViewClicked() {
        if (!etAccount.getText().toString().trim().equals("") && !etPassword.getText().toString().trim().equals("")) {
            Map<String, String> map = new HashMap<>();
            map.put("phone", etAccount.getText().toString().trim());
            map.put("password", etPassword.getText().toString().trim());
            mPresenter.userLogin(map);
        } else {
            ToastUtil.displayShortToast("请输入账号或者密码");
        }
    }

    @OnClick({R.id.clear_phone, R.id.clear_psd, R.id.avater, R.id.checkview})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.clear_phone:
                etAccount.setText("");
                break;
            case R.id.clear_psd:
                etPassword.setText("");
                break;
            case R.id.avater:
                checkview.setVisibility(View.VISIBLE);
                break;
            case R.id.checkview:
                if (clickCount == 0) {
                    fiveClickHandlere.sendEmptyMessageDelayed(0, 1000);
                }
                clickCount++;
                break;
        }
    }
    Handler fiveClickHandlere = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (clickCount >= 4) {
                check.setVisibility(View.VISIBLE);
            }
            clickCount = 0;
        }
    };
}